package com.csxh.web.handler.eshop;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.csxh.web.mapper.eshop.ProductMapper;
import com.csxh.web.model.eshop.Customer;
import com.csxh.web.model.eshop.Order;
import com.csxh.web.model.eshop.Paymethod;
import com.csxh.web.model.eshop.Product;
import com.csxh.web.model.eshop.Shipping;
import com.csxh.web.service.eshop.OrderService;
import com.csxh.web.service.eshop.OrderdetailService;
import com.csxh.web.service.eshop.PaymethodService;
import com.csxh.web.service.eshop.ProductService;
import com.csxh.web.service.eshop.ShippingService;
import com.csxh.web.util.WebUtil;

@Controller
public class OrderHandler {

	
	@Autowired
	private OrderService orderService;
	@Autowired
	private OrderdetailService orderdetailService;
	
	@Autowired
	private ShippingService shippingService;
	@Autowired
	private PaymethodService paymethodService;
	
	@RequestMapping("/order")
	public String showOrderForm(HttpServletRequest request) {
		//准备送货数据
	List<Shipping> shippingList=shippingService.findAll();
	request.setAttribute("shippingList", shippingList);	
	
		
		//准备支付方式数据
	List<Paymethod> paymethodList=paymethodService.findAll();
	request.setAttribute("paymethodList", paymethodList);	
	
		
		return "order";
	}
	
	
	@RequestMapping("/order/add")
	public String submitOrderForm(HttpServletRequest request) {
		
		Customer c=WebUtil.getUser(request);
		
		Order order=new Order();
		order.setCustomerId(c.getId());
		order.setDeliverAddres(c.getAddress());
		order.setDeliverCity(c.getCity());
		order.setDeliverName(c.getRealname());
		order.setDeliverPhone(c.getPhone());
		order.setDeliverZip(c.getZip());
		order.setFulfilled(false);
		
		
		orderService.save(order);
		
		return "order";
	}
}
